package com.hymall.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketComment;
import com.hymall.bean.MarketCommentExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxCommentCountVO;
import com.hymall.mapper.MarketCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description 评论模块 接口实现
 * @date 2022/7/19 21:58
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public WxCommentCountVO count(Integer valueId, Byte type) {
        WxCommentCountVO vo = new WxCommentCountVO();
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        Long countAll = marketCommentMapper.countByExample(example);
        criteria.andHasPictureEqualTo(true);
        Long countPic = marketCommentMapper.countByExample(example);
        vo.setAllCount(countAll.intValue());
        vo.setHasPicCount(countPic.intValue());
        return vo;
    }

    @Override
    public CommonData<MarketComment> list(Integer valueId, Byte type, BasePageInfo info, Integer showType) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);

        List<MarketComment> list = marketCommentMapper.selectByExample(example, "ASC");
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public void post(MarketComment marketComment) {
        // 父类赋值给子类
        int code = marketCommentMapper.insertSelective(marketComment);
    }
}
